package com.easyim.rest.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Value;
import org.neo4j.driver.types.Node;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@Data
public class NeoUtils {

    @Autowired
    Session neo4jSession;

    /**
     *  新建neo4j user node 节点
     *  建立一个关注关系 移除关注关系
     *
     */

    public void execQuery(){
        String cypherQuery = "MATCH (u:UserRelation {userId: $userId}) RETURN u";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("userId", 10086);
        Result result = neo4jSession.run(cypherQuery, parameters);

    }


    public Boolean addFollowing(Long fromId, Long toId){
        String addFollowCypher =
                "MATCH (follower:UserRelation {userId: $fromId}) MATCH (following:UserRelation {userId: $toId}) MERGE (follower)-[:FOLLOWING]->(following)";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("fromId", fromId);
        parameters.put("toId", toId);
        //执行查询
        try{
            neo4jSession.run(addFollowCypher, parameters);
            return Boolean.TRUE;
        }catch (Exception e){
            log.error("save user-> {} follows {} relation fail", fromId, toId, e);
        }
        return Boolean.FALSE;
    }


    /**
     *  获取我的关注列表
     * @param userId
     * @return
     */
    public List<Long> getMyFollowing(Long userId){
        String getMyFollows = "MATCH (u:UserRelation {userId: $userId})-[:FOLLOWING]->(following:UserRelation) RETURN following";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("userId", userId);
        //执行查询
        try{
            Result myFollowing = neo4jSession.run(getMyFollows, parameters);
            return dealMyFollowingResult(myFollowing);
        }catch (Exception e){
            log.error("get user {} ->following fail", userId, e);
        }
        return null;
    }

    public List<Long> dealMyFollowingResult(Result result){
        List<Long> followingIdList = new LinkedList<>();
        while (result.hasNext()) {
            Record record = result.next();
            Node userNode = record.get("following").asNode();
            Value userId = userNode.get("userId");
            long followingId = userId.asLong();
            followingIdList.add(followingId);
        }
        return followingIdList;
    }


    public List<Long> getFollowsMe(Long userId){
        String followMe = "MATCH (user:UserRelation)-[r:FOLLOWING]->(following:UserRelation {userId: $userId}) RETURN user";
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("userId", userId);
        //执行查询
        try{
            Result myFollowing = neo4jSession.run(followMe, parameters);
            return dealFollowingMeResult(myFollowing);
        }catch (Exception e){
            log.error("get user folowing  {} -> fail", userId, e);
        }
        return null;
    }


    public List<Long> dealFollowingMeResult(Result result){
        List<Long> followingIdList = new LinkedList<>();
        while (result.hasNext()) {
            Record record = result.next();
            Node userNode = record.get("user").asNode();
            Value userId = userNode.get("userId");
            long followingId = userId.asLong();
            followingIdList.add(followingId);
        }
        return followingIdList;
    }
}
